package com.stack.auto_parts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stack.auto_parts.entity.Orders;

import com.stack.auto_parts.entity.SupplierOrder;
import com.stack.auto_parts.mapper.OrderMapper;
import com.stack.auto_parts.service.OrderService;
import com.stack.auto_parts.service.SupplierOrderService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Title: OrderServiceImpl
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.service.impl
 * @Date 2024/7/9 11:11
 * @description:
 */

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Resource
    SupplierOrderService supplierOrderService;

    @Override
    public void applyOrderr(Orders orders) {
        orders.setUuid(UUID.randomUUID().toString());

        System.out.println("orders:" + orders);
        Integer integer = orderMapper.insert(orders);
    }

    @Override
    public String deleteOrderr(String uuid) {
        // QueryWrapper 条件构造器
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",uuid);
        if(orderMapper.selectOne(queryWrapper) == null){
            return "该订单不存在";
        }
        else {
            Integer integer = orderMapper.deleteById(orderMapper.selectOne(queryWrapper));
            if(integer != 1)
                return "订单删除失败";
        }
        return null;
    }

    @Override
    public List<Orders> list() {
        return orderMapper.selectList(null);
    }

    @Override
    public List<Orders> selectByCondition(String purchase_name,String partName, String supplier, String uuid, String state) {
        List<Orders> ordersList = orderMapper.listByCondition(purchase_name,partName,supplier,uuid,state);
        return ordersList;
    }

    /**
     * 财务员审核
     */
    @Override
    public String statues(String uuid,String state,String userName) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",uuid);
        if(orderMapper.selectOne(queryWrapper) == null){
            return "该订单不存在";
        }
        else {
            Orders order = orderMapper.selectOne(queryWrapper);
            order.setState(state);
            order.setApprover(userName);
            Integer integer = orderMapper.updateById(order);
            if(integer != 1)
                return "订单审核失败";
            if(Objects.equals(state, "0")){
                SupplierOrder supplierOrder = new SupplierOrder(null,order.getUuid(),order.getPartName(),order.getNumber(),order.getTotalPrice(),order.getSupplier(),null,0,0,null,null,null);
                supplierOrderService.insertSupplierOrder(supplierOrder);
            } else if (Objects.equals(state, "1")) {
                return null;
            }
        }

        return null;
    }

    @Override
    public String manger(String uuid) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",uuid);
        Orders order = orderMapper.selectOne(queryWrapper);
        if(order == null){
            return "该订单不存在";
        }
        else {
            order.setState("1");
            Integer integer = orderMapper.updateById(order);
            if(integer != 1)
                return "订单审核失败";

            supplierOrderService.seterror(uuid);
        }

        return null;
    }

    @Override
    public List<Map<String, Object>> getputdate() {

        return null;
    }


}
